

//
//  AppLaunchBannerViewController.m
//  zhijiansuzhou
//
//  Created by 度周末网络-王腾 on 16/5/24.
//  Copyright © 2016年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "AppLaunchBannerViewController.h"

@interface AppLaunchBannerViewController ()


@end

@implementation AppLaunchBannerViewController

+(instancetype)sharedGuide {
    static dispatch_once_t onceToken;
    static AppLaunchBannerViewController *instance;
    dispatch_once(&onceToken, ^{
        instance = [[UIStoryboard storyboardWithName:@"AppLaunchBanner" bundle:nil] instantiateInitialViewController];
    });
    return instance;
}


+(void)afterDelay:(NSTimeInterval )timerInterval block:(void (^)())block{
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timerInterval*NSEC_PER_SEC));
    dispatch_after(time, dispatch_get_main_queue(), ^{
        block();
    });
}

+ (void)show
{
   
  
    [HttpNetWorkUtil requestBanner:nil success:^(id json) {
        
    
       
        NSString *urlString = [[json objectForKey:@"data"] objectForKey:@"imgPath"];
        
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urlString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                
                if (image) {
                    
                    
                    [AppLaunchBannerViewController sharedGuide].img.image = image;
 
                    [[AppLaunchBannerViewController sharedGuide] showGuide];
                    
                    
                    [AppLaunchBannerViewController afterDelay:2 block:^{
                         [[AppLaunchBannerViewController sharedGuide] hideGuide];
                    }];
                }else{
                    [[AppLaunchBannerViewController sharedGuide] hideGuide];
                }
            }];
          
    
        
    } fail:^{
        [[AppLaunchBannerViewController sharedGuide] hideGuide];
    }];
 
    [[AppLaunchBannerViewController sharedGuide] showGuide];

}

+ (void)hide
{
    [[AppLaunchBannerViewController sharedGuide] hideGuide];
}

- (UIWindow *)mainWindow
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app.delegate respondsToSelector:@selector(window)]){
        return [app.delegate window];
    }
    else{
        return [app keyWindow];
    }
}

- (void)showGuide{
    if (self.view.superview == nil){
        [[self mainWindow] addSubview:[AppLaunchBannerViewController sharedGuide].view];
        [AppLaunchBannerViewController sharedGuide].view.alpha = 1.0f;
        [self guideShown];
        
    }
}

- (void)guideShown
{
    [self mainWindow].backgroundColor = [UIColor clearColor];
}

- (void)hideGuide
{
    
    if ( self.view.superview != nil){
        [AppLaunchBannerViewController sharedGuide].view.alpha = 1.0;
        [UIView animateWithDuration:0.8 animations:^{
            [AppLaunchBannerViewController sharedGuide].view.alpha = 0.0;
        } completion:^(BOOL finished) {
            [[[AppLaunchBannerViewController sharedGuide] view] removeFromSuperview];
        }];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    if (IS_IPHONE4S) {
     
        self.backgroundImg.image = [UIImage imageNamed:@"b4"];
        self.img_bottom_layout.constant = 74;
        self.bimg.image = [UIImage imageNamed:@"d4"];
    }else if (IS_IPHONE5){
        self.img_bottom_layout.constant = 77;
        
    }else if (IS_IPHONE6){
         self.img_bottom_layout.constant = 90;
        
    }else if (IS_IPHONE6ORLATER){
        self.img_bottom_layout.constant = 100;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
